<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 连接测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f0f0f0;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
            font-weight: bold;
        }
        .connected {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .disconnected {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .connecting {
            background-color: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .log {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 10px;
            height: 300px;
            overflow-y: auto;
            font-family: monospace;
            font-size: 12px;
        }
        .aircraft-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 10px;
            margin-top: 20px;
        }
        .aircraft-card {
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 10px;
            background-color: #f8f9fa;
        }
        .aircraft-card h3 {
            margin: 0 0 10px 0;
            color: #495057;
        }
        .aircraft-card p {
            margin: 5px 0;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WebSocket 飞机轨迹测试</h1>
        
        <div id="status" class="status disconnected">
            状态: 未连接
        </div>
        
        <div>
            <button id="connectBtn" onclick="connect()">连接服务器</button>
            <button id="disconnectBtn" onclick="disconnect()" disabled>断开连接</button>
            <button onclick="clearLog()">清空日志</button>
        </div>
        
        <h3>连接信息</h3>
        <div class="log" id="log"></div>
        
        <h3>飞机信息</h3>
        <div id="aircraftInfo" class="aircraft-info"></div>
    </div>

    <script>
        let ws = null;
        let aircrafts = {};
        
        const wsUrl = 'ws://127.0.0.1:6088/uam/v2/ws/uam/channel/245';
        
        function log(message) {
            const logDiv = document.getElementById('log');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `[${timestamp}] ${message}\n`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        function updateStatus(status, className) {
            const statusDiv = document.getElementById('status');
            statusDiv.textContent = `状态: ${status}`;
            statusDiv.className = `status ${className}`;
        }
        
        function updateButtons(connected) {
            document.getElementById('connectBtn').disabled = connected;
            document.getElementById('disconnectBtn').disabled = !connected;
        }
        
        function connect() {
            try {
                log(`正在连接到: ${wsUrl}`);
                updateStatus('连接中...', 'connecting');
                
                ws = new WebSocket(wsUrl);
                
                ws.onopen = function(event) {
                    log('WebSocket 连接已建立');
                    updateStatus('已连接', 'connected');
                    updateButtons(true);
                };
                
                ws.onmessage = function(event) {
                    const data = JSON.parse(event.data);
                    log(`收到数据: ${JSON.stringify(data, null, 2)}`);
                    handleMessage(data);
                };
                
                ws.onclose = function(event) {
                    log(`WebSocket 连接已关闭: ${event.code} - ${event.reason}`);
                    updateStatus('已断开', 'disconnected');
                    updateButtons(false);
                    ws = null;
                };
                
                ws.onerror = function(error) {
                    log(`WebSocket 错误: ${error}`);
                    updateStatus('连接错误', 'disconnected');
                    updateButtons(false);
                };
                
            } catch (error) {
                log(`连接失败: ${error.message}`);
                updateStatus('连接失败', 'disconnected');
                updateButtons(false);
            }
        }
        
        function disconnect() {
            if (ws) {
                ws.close();
            }
        }
        
        function clearLog() {
            document.getElementById('log').innerHTML = '';
        }
        
        function handleMessage(data) {
            switch (data.type) {
                case 'init':
                    log('收到初始化数据');
                    updateAircrafts(data.aircrafts);
                    break;
                case 'update':
                    log('收到更新数据');
                    updateAircrafts(data.aircrafts);
                    break;
                default:
                    log(`未知消息类型: ${data.type}`);
            }
        }
        
        function updateAircrafts(aircraftsData) {
            aircrafts = {};
            const container = document.getElementById('aircraftInfo');
            container.innerHTML = '';
            
            aircraftsData.forEach(aircraft => {
                aircrafts[aircraft.aircraftId] = aircraft;
                
                const card = document.createElement('div');
                card.className = 'aircraft-card';
                card.innerHTML = `
                    <h3>${aircraft.name} (${aircraft.aircraftId})</h3>
                    <p><strong>位置:</strong> ${aircraft.latitude.toFixed(4)}, ${aircraft.longitude.toFixed(4)}</p>
                    <p><strong>高度:</strong> ${aircraft.altitude.toFixed(0)}m</p>
                    <p><strong>航向:</strong> ${aircraft.heading}°</p>
                    <p><strong>速度:</strong> ${aircraft.speed} km/h</p>
                    <p><strong>轨迹点:</strong> ${aircraft.trackPoints ? aircraft.trackPoints.length : 0}</p>
                    <p><strong>时间:</strong> ${new Date(aircraft.timestamp).toLocaleTimeString()}</p>
                `;
                container.appendChild(card);
            });
        }
        
        // 页面加载时显示连接信息
        log(`WebSocket URL: ${wsUrl}`);
        log('点击"连接服务器"按钮开始测试');
    </script>
</body>
</html>
